package com.lucfzy.rest.controller.geo.impl;

import com.lucfzy.log.producer.AppLogger;
import com.lucfzy.rest.controller.geo.GaoControllerInter;
import com.lucfzy.rest.dto.GeoRootDTO;
import com.lucfzy.rest.dto.Geocodes;
import com.lucfzy.rest.dto.regeo.AddressDetail;
import com.lucfzy.rest.dto.response.GeoInfoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Objects;

@CrossOrigin
@RestController
public class GaoController implements GaoControllerInter {

    @Value("${version}")
    private String version;
    @Value("${key}")
    private String key;


    @Autowired
    private AppLogger appLogger;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public GeoInfoResponse getInfo(String address, String city) {
        appLogger.info("开始记录查询信息, address: " + address + "-city: " + city);
        // 处理city信息，并返回正确的city
        String correctCityInfo = dealCityInfo(city);
        GeoRootDTO geoRootDTO = restTemplate.getForObject("https://restapi.amap.com/" + version + "/geocode/geo?key=" + key + "&address=" + address + "&city=" + correctCityInfo + "", GeoRootDTO.class);
        // 响应体
        GeoInfoResponse response = getGeoInfoResponse(geoRootDTO);
        // 基本对象校验
        if (!Objects.isNull(response)) {
            return response;
        }
        throw new RuntimeException("获取地理位置信息错误，请检查。");
    }

    /**
     * 处理响应体，转换
     *
     * @Author liu-miss/lucfz
     * @Description 处理响应体，转换
     * @Date 2021/7/25 8:43
     * @param geoRootDTO 参数实体
     * @return com.lucfzy.rest.dto.response.GeoInfoResponse
     **/
    private GeoInfoResponse getGeoInfoResponse(GeoRootDTO geoRootDTO) {
        GeoInfoResponse response = GeoInfoResponse.builder().build();
        if (!Objects.isNull(geoRootDTO) && !CollectionUtils.isEmpty(geoRootDTO.getGeocodes())) {
            List<Geocodes> geocodes = geoRootDTO.getGeocodes();
            if (CollectionUtils.isEmpty(geocodes)) {
                response.setStatus("失败");
                return response;
            }
            Geocodes apiReturn = geocodes.get(0);
            response.setStatus("成功");
            response.setInfocode(geoRootDTO.getInfocode());
            response.setProvince(apiReturn.getProvince());
            response.setCity(apiReturn.getCity());
            response.setZone(apiReturn.getDistrict());
            if (!CollectionUtils.isEmpty(apiReturn.getNeighborhood().getName())) {
                response.setCommunity(apiReturn.getNeighborhood().getName().get(0));
            }
            response.setCityCode(apiReturn.getCitycode());
            String location = apiReturn.getLocation();
            // 返回街道信息
            String townShip = this.getReGeoAddress(location);
            response.setTownShip(townShip);
            return response;
        }
        return null;
    }

    /**
     * 返回拼接后的正确的city信息,注意只取市名字，取省份数据不对
     *
     * @param cityInput
     * @return java.lang.String
     * @Author liu-miss/lucfz
     * @Description //TODO
     * @Date 2021/7/24 1:56
     **/
    private String dealCityInfo(String cityInput) {
        String[] split = cityInput.split(",");
        for (int i = split.length - 1; i >= 0; i--) {
            return split[i];
        }
        return null;
    }

    /**
     * 根据location定位街道信息
     *
     * @param location 经纬度信息
     * @return com.lucfzy.rest.dto.regeo.Address 根据location定位街道信息
     * @Author liu-miss/lucfz
     * @Description 根据location定位街道信息
     * @Date 2021/7/22 12:57
     **/
    public String getReGeoAddress(String location) {
        AddressDetail resultInfo = restTemplate.getForObject("https://restapi.amap.com/v3/geocode/regeo?location=" + location + "&key=" + key, AddressDetail.class);
        if (!Objects.isNull(resultInfo.getRegeocode())) {
            AddressDetail.Regeocode regeocode = resultInfo.getRegeocode();
            return regeocode.getAddressComponent().getTownship();
        }
        return "";
    }

}
